﻿////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// <copyright>Copyright 2008-2011 Andreas Huber Doenni</copyright>
// Distributed under the GNU General Public License version 2 (GPLv2).
// See accompanying file License.txt or copy at http://phuse.codeplex.com/license.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

namespace Phuse.Net.Nntp
{
    using System;
    using System.IO;

    using Phuse.Text;

    /// <summary>Represents an STAT command to be sent to an NNTP server, see
    /// <a href="http://www.ietf.org/rfc/rfc3977.txt">RFC 3977</a> 6.2.4.</summary>
    /// <threadsafety static="true" instance="false"/>
    public sealed class StatCommand : SingleLineCommand<StatResponse, long, string>
    {
        private static readonly string[] SuccessResponseCodes = { "223" };

        /// <summary>Initializes a new instance of the <see cref="StatCommand"/> class.</summary>
        /// <exception cref="ArgumentNullException"><paramref name="messageId"/> equals <c>null</c>.</exception>
        /// <exception cref="ArgumentException"><paramref name="messageId"/> contains white space.</exception>
        public StatCommand(string messageId) : this(" ", messageId)
        {
            CommandHelper.AssertContainsNoWhiteSpace("messageId", messageId);
        }

        /// <summary>Initializes a new instance of the <see cref="StatCommand"/> class.</summary>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="number"/> is smaller than or equal to 0.
        /// </exception>
        public StatCommand(long number) : this(" ", StringHelper.ToString(number))
        {
            if (number <= 0)
            {
                throw new ArgumentOutOfRangeException("number", ExceptionMessages.PositiveNumberRequired);
            }
        }

        /// <summary>Initializes a new instance of the <see cref="StatCommand"/> class.</summary>
        public StatCommand() : this(null, null)
        {
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        /// <summary>See <see cref="SingleLineCommandBase{T, U, V, W}.CreateResponse(V, W, Stream)"/>.</summary>
        protected sealed override StatResponse CreateResponse(long p1, string p2, Stream subsequentLines)
        {
            return new StatResponse(p1, p2);
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        private StatCommand(string separator, string argument) :
            base("STAT" + separator + argument, SuccessResponseCodes)
        {
        }
    }
}
